@echo off
setlocal enabledelayedexpansion

echo 🏗️ AI_CTP Conda + VS 自动编译脚本
echo ================================
echo 自动设置Visual Studio环境 + Conda环境
echo.

REM 设置UTF-8编码
chcp 65001 >nul 2>&1

REM 设置conda环境路径
set CONDA_ENV_PATH=E:\my_env
echo 🐍 使用Conda环境: %CONDA_ENV_PATH%

REM 检查conda环境是否存在
if not exist "%CONDA_ENV_PATH%" (
    echo ❌ Conda环境不存在: %CONDA_ENV_PATH%
    pause
    exit /b 1
)

REM 自动查找并设置Visual Studio环境
echo 🔍 自动查找Visual Studio环境...

set VS_FOUND=0

REM 尝试VS 2022 Community
if exist "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" (
    echo ✓ 找到VS 2022 Community
    call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
    set VS_FOUND=1
    goto :vs_found
)

REM 尝试VS 2022 Professional
if exist "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat" (
    echo ✓ 找到VS 2022 Professional
    call "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat"
    set VS_FOUND=1
    goto :vs_found
)

REM 尝试VS 2022 Enterprise
if exist "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat" (
    echo ✓ 找到VS 2022 Enterprise
    call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
    set VS_FOUND=1
    goto :vs_found
)

REM 尝试VS 2019 Community
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" (
    echo ✓ 找到VS 2019 Community
    call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
    set VS_FOUND=1
    goto :vs_found
)

REM 尝试Build Tools for Visual Studio 2022
if exist "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" (
    echo ✓ 找到VS 2022 Build Tools
    call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
    set VS_FOUND=1
    goto :vs_found
)

if %VS_FOUND%==0 (
    echo ❌ 未找到Visual Studio环境
    echo.
    echo 请安装以下之一:
    echo 1. Visual Studio 2022 Community (推荐)
    echo 2. Visual Studio 2019 Community
    echo 3. Build Tools for Visual Studio 2022
    echo.
    pause
    exit /b 1
)

:vs_found
echo ✅ Visual Studio环境设置成功

REM 验证MSVC编译器
cl.exe >nul 2>&1
if errorlevel 1 (
    echo ❌ MSVC编译器验证失败
    pause
    exit /b 1
)
echo ✅ MSVC编译器验证成功

REM 设置conda环境
echo 🔄 设置conda环境...
set PATH=%CONDA_ENV_PATH%;%CONDA_ENV_PATH%\Scripts;%CONDA_ENV_PATH%\Library\bin;%PATH%
set PYTHONPATH=%CONDA_ENV_PATH%\Lib\site-packages;%PYTHONPATH%

REM 验证Python环境
"%CONDA_ENV_PATH%\python.exe" --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Conda Python验证失败
    pause
    exit /b 1
)

REM 显示环境信息
echo ✅ 环境设置完成
echo.
for /f "tokens=*" %%i in ('"%CONDA_ENV_PATH%\python.exe" --version 2^>^&1') do set PYTHON_VERSION=%%i
for /f "tokens=*" %%i in ('"%CONDA_ENV_PATH%\python.exe" -c "import pybind11; print(pybind11.__version__)"') do set PYBIND11_VERSION=%%i
echo 📋 环境信息:
echo    Conda环境: %CONDA_ENV_PATH%
echo    %PYTHON_VERSION%
echo    pybind11: %PYBIND11_VERSION%
echo    工作目录: %CD%
echo.

REM 检查pybind11
echo 🐍 检查pybind11...
"%CONDA_ENV_PATH%\python.exe" -c "import pybind11" >nul 2>&1
if errorlevel 1 (
    echo ❌ pybind11未安装，正在安装...
    "%CONDA_ENV_PATH%\Scripts\pip.exe" install pybind11
    if errorlevel 1 (
        echo ❌ pybind11安装失败
        pause
        exit /b 1
    )
    echo ✅ pybind11安装成功
)

REM 清理旧构建
echo 🧹 清理构建环境...
if exist build rmdir /s /q build
if exist install rmdir /s /q install
if exist *.pyd del /q *.pyd

REM 创建构建目录
mkdir build
mkdir install

REM 进入构建目录
cd build

REM 获取Python路径信息
set PYTHON_EXECUTABLE=%CONDA_ENV_PATH%\python.exe
set PYTHON_PREFIX=%CONDA_ENV_PATH%
for /f "tokens=*" %%i in ('"%CONDA_ENV_PATH%\python.exe" -c "import pybind11; print(pybind11.get_cmake_dir())"') do set PYBIND11_DIR=%%i

echo 📋 Python配置:
echo    可执行文件: %PYTHON_EXECUTABLE%
echo    前缀目录: %PYTHON_PREFIX%
echo    pybind11目录: %PYBIND11_DIR%
echo.

REM CMake配置 - 使用conda环境
echo ⚙️ CMake配置...
cmake .. ^
    -G "Visual Studio 17 2022" ^
    -A x64 ^
    -DCMAKE_BUILD_TYPE=Release ^
    -DCMAKE_INSTALL_PREFIX=../install ^
    -DPython_EXECUTABLE="%PYTHON_EXECUTABLE%" ^
    -DPython_ROOT_DIR="%PYTHON_PREFIX%" ^
    -Dpybind11_DIR="%PYBIND11_DIR%"

if errorlevel 1 (
    echo ❌ CMake配置失败
    echo.
    echo 💡 可能的问题:
    echo 1. CMakeLists.txt配置错误
    echo 2. Python开发头文件缺失
    echo 3. pybind11配置问题
    echo.
    cd ..
    pause
    exit /b 1
)

echo ✅ CMake配置成功
echo.

REM 编译项目
echo 🔨 编译项目...
cmake --build . --config Release --parallel

if errorlevel 1 (
    echo ❌ 编译失败
    echo.
    echo 💡 检查编译错误信息
    cd ..
    pause
    exit /b 1
)

echo ✅ 编译成功
echo.

REM 安装项目
echo 📦 安装项目...
cmake --install . --config Release

if errorlevel 1 (
    echo ❌ 安装失败
    cd ..
    pause
    exit /b 1
)

echo ✅ 安装成功
echo.

cd ..

REM 复制文件到根目录
echo 📋 复制文件...
if exist "build\Release\ai_ctp.*.pyd" (
    copy "build\Release\ai_ctp.*.pyd" . >nul 2>&1
    echo ✅ 已复制.pyd文件到根目录
)

REM 复制DLL文件
if exist "build\Release\*.dll" (
    copy "build\Release\*.dll" . >nul 2>&1
    echo ✅ 已复制DLL文件到根目录
)

REM 测试模块
echo 🧪 测试模块...
set PYTHONPATH=%CD%\install;%CD%;%PYTHONPATH%

"%CONDA_ENV_PATH%\python.exe" -c "import ai_ctp; print(f'✅ AI_CTP {ai_ctp.__version__} 加载成功')" 2>nul
if errorlevel 1 (
    echo ❌ 模块测试失败
    echo.
    echo 📁 检查生成的文件:
    if exist "*.pyd" dir "*.pyd"
    if exist "*.dll" dir "*.dll"
    if exist "build\Release\*.*" dir "build\Release\*.*"
    echo.
    pause
    exit /b 1
)

REM 显示编译结果
echo.
echo 🎉 Conda + VS 编译完成！
echo ================================
echo 📦 安装位置: %CD%\install
echo 📋 根目录文件: %CD%
echo.

echo 📁 生成的文件:
if exist "*.pyd" (
    for %%f in (*.pyd) do echo    ✅ %%f
)
if exist "*.dll" (
    for %%f in (*.dll) do echo    ✅ %%f
)
echo.

echo 🚀 使用方法:
echo    1. 基础测试: "%CONDA_ENV_PATH%\python.exe" test_ai_ctp.py
echo    2. K线记录: "%CONDA_ENV_PATH%\python.exe" kline_recorder.py  
echo    3. 企业示例: "%CONDA_ENV_PATH%\python.exe" enterprise_example.py
echo.
echo 💡 或者激活conda环境后直接使用python:
echo    call "%CONDA_ENV_PATH%\Scripts\activate.bat"
echo    python test_ai_ctp.py
echo.

echo 💡 编译成功！可以开始使用AI_CTP了
echo.

pause 